package com.ruoyi.web.controller.system;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RecZmPaperInsideManagement;
import com.ruoyi.system.domain.RecZmPaperManagementD;
import com.ruoyi.system.domain.RecordProp;
import com.ruoyi.system.service.IRecZmPaperManagementDService;
import com.ruoyi.system.service.IRecordPropService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.web.controller.tool.ExcelUtilMap;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专门档案  独立的Controller
 *
 * @author yzf
 * @date 2022-06-03
 */
@RestController
@RequestMapping("/system/RecZmPaperManagementD")
public class RecZmPaperManagementDController extends BaseController {
    @Autowired
    private IRecZmPaperManagementDService recZmPaperManagementDService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    IRecordPropService iRecordPropService;

    /**
     * 查询专门档案  独立的列表
     */
    @PreAuthorize("@ss.hasPermi('system:RecZmPaperManagementD:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecZmPaperManagementD recZmPaperManagementD) {
        startPage();
        List<RecZmPaperManagementD> list = recZmPaperManagementDService.selectRecZmPaperManagementDList(recZmPaperManagementD);
        return getDataTable(list);
    }

    /****
     *根据题名查询案卷
     * @param recZmPaperManagementD
     * @return
     */
    @GetMapping("/listByName")
    public AjaxResult listByName(RecZmPaperManagementD recZmPaperManagementD) {
        List<Map<String, Object>> list = recZmPaperManagementDService.selectlistByName(recZmPaperManagementD);
        return AjaxResult.success(list);
    }

    /**
     * 获取专门档案  独立的详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:RecZmPaperManagementD:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(recZmPaperManagementDService.selectRecZmPaperManagementDById(id));
    }

    /**
     * 新增专门档案  独立的
     */
    @PreAuthorize("@ss.hasPermi('system:RecZmPaperManagementD:add')")
    @Log(title = "专门档案  独立的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecZmPaperManagementD recZmPaperManagementD) {
        //件号
        //recZmPaperManagementD.setPartNumber(sysConfigService.partNumberConfigByKey("zm_part_number"));
        return toAjax(recZmPaperManagementDService.insertRecZmPaperManagementD(recZmPaperManagementD));
    }

    /**
     * 修改专门档案  独立的
     */
    @PreAuthorize("@ss.hasPermi('system:RecZmPaperManagementD:edit')")
    @Log(title = "专门档案  独立的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecZmPaperManagementD recZmPaperManagementD) {
        return toAjax(recZmPaperManagementDService.updateRecZmPaperManagementD(recZmPaperManagementD));
    }

    /**
     * 修改专门档案 批量文件上传
     */
    @PreAuthorize("@ss.hasPermi('system:RecZmPaperManagementD:multFile')")
    @Log(title = "专门档案 独立文件 批量文件上传", businessType = BusinessType.UPDATE)
    @PostMapping("/multFile")
    public AjaxResult multFile(@RequestBody RecZmPaperManagementD recZmPaperManagementD) {
        int i = recZmPaperManagementDService.upLoadMultFile(recZmPaperManagementD);
        if(i > 0){
            return AjaxResult.success("成功上传" + i + "条数据");
        }else {
            return AjaxResult.error("上传失败，请检查挂载序号！");
        }
    }

    /**
     * 删除专门档案  独立的
     */
    @PreAuthorize("@ss.hasPermi('system:RecZmPaperManagementD:remove')")
    @Log(title = "专门档案  独立的", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(recZmPaperManagementDService.deleteRecZmPaperManagementDByIds(ids));
    }

    /**
     * 案卷归档申请
     */
    @Log(title = "案卷归档申请", businessType = BusinessType.UPDATE)
    @PutMapping("/apply/{ids}")
    public AjaxResult updateArchivesApply(@PathVariable String[] ids) {
        return toAjax(recZmPaperManagementDService.updateArchivesApplyByIds(ids));
    }

    /**
     * 案卷归档通过
     */
    @Log(title = "案卷归档通过", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{ids}")
    public AjaxResult updateArchivesApprove(@PathVariable String[] ids) {
        return toAjax(recZmPaperManagementDService.updateArchivesApprove(ids));
    }

    /**
     * 导出专门档案  独立的列表
     */
    @PreAuthorize("@ss.hasPermi('system:RecZmPaperManagementD:export')")
    @Log(title = "专门档案  独立的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecZmPaperManagementD recZmPaperManagementD) {
        List<RecZmPaperManagementD> list = recZmPaperManagementDService.selectRecZmPaperManagementDList(recZmPaperManagementD);
        ExcelUtil<RecZmPaperManagementD> util = new ExcelUtil<RecZmPaperManagementD>(RecZmPaperManagementD.class);
        util.exportExcel(response, list, "专门档案  独立的数据");
    }

    /**
     * 文件导入
     */
    @Log(title = "文件导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:file:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RecZmPaperManagementD> util = new ExcelUtil<RecZmPaperManagementD>(RecZmPaperManagementD.class);
        List<RecZmPaperManagementD> fileList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = recZmPaperManagementDService.importFile(fileList, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RecZmPaperManagementD> util = new ExcelUtil<RecZmPaperManagementD>(RecZmPaperManagementD.class);
        util.importTemplateExcel(response, "项目工程数据");
    }

    /**
     * 案卷归档拒绝
     */
    @Log(title = "案卷归档拒绝", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{ids}")
    public AjaxResult updateArchivesReject(@PathVariable String[] ids) {
        return toAjax(recZmPaperManagementDService.updateArchivesReject(ids));
    }

    /**
     * 导出文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "案卷包导出", businessType = BusinessType.EXPORT)
    @PostMapping("/anJuanBaoExport/{archivesId}")
    public void anJuanBaoExport(@PathVariable("archivesId") String archivesId, HttpServletRequest request, HttpServletResponse response) {
        //生成文件并反回
        File resfile = recZmPaperManagementDService.selectRecordFileExport(archivesId);
        if (ObjectUtil.isEmpty(resfile)) {
            return;
        }
        try {
            String downloadName = "案卷.zip";
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            // FileUtils.writeBytes(resfile.getAbsolutePath(), response.getOutputStream());
            FileUtil.writeToStream(resfile, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            resfile.delete();
        }

    }
}
